/*
 * @Author: szx
 * @Date: 2022-03-23 14:39:14
 * @LastEditTime: 2022-03-23 15:10:42
 * @Description:
 * @FilePath: \leetcode\剑指offer\35\35.js
 */
function Node(val, next, random) {
    this.val = val;
    this.next = next;
    this.random = random;
}

/**
 * @param {Node} head
 * @return {Node}
 */
var copyRandomList = function (head) {
    if (!head) return head;
    for (let node = head; node != null; node = node.next.next) {
        const newNode = new Node(node.val, node.next);
        node.next = newNode;
    }
    let newHead = head.next;
    for (let node = head; node != null; node = node.next) {
        const newNode = node.next;
        newNode.random = node.random == null ? null : node.random.next;
        if (!newNode.next) {
            node.next = null;
            newNode.next = null;
            break;
        }
        node.next = node.next.next;
        newNode.next = newNode.next.next;
    }
    return newHead;
};
